package com.zou.practice.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author zouyaowen
 * @date 2020-09-08
 */
public class ProxyTest {
    public static void main(String[] args) {
        ServiceImpl target = new ServiceImpl();
        Service service = (Service) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                long startTime = System.currentTimeMillis();
                // 使用反射执行委托类对象具体方法
                Object result = method.invoke(target, args);
                System.out.println(method.getName() + " Time is:" + (System.currentTimeMillis() - startTime));
                return result;
            }
        });
        service.sale();
    }
}
